package com.report.opportunity.domain;

import com.report.commons.events.opportunity.OpportunityCreatedEvent;
import com.report.opportunity.command.CreateOpportunityCommand;
import org.axonframework.commandhandling.CommandHandler;
import org.axonframework.commandhandling.model.AggregateIdentifier;
import org.axonframework.eventhandling.EventHandler;
import org.axonframework.spring.stereotype.Aggregate;

import static org.axonframework.commandhandling.model.AggregateLifecycle.apply;

@Aggregate
public class OpportunityAggregate{

    @AggregateIdentifier
    private String id;
    private String name;

    @CommandHandler
    public OpportunityAggregate(CreateOpportunityCommand command){
        apply(new OpportunityCreatedEvent(command.getId() , command.getName()));
    }

    @EventHandler
    public void on(OpportunityCreatedEvent event){
        this.id = event.getId();
        this.name = event.getName();
    }
}
